package com.megvii.koala.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.megvii.koala.R;
import com.megvii.koala.components.LimitEditText;
import com.megvii.koala.data.bean.CreatureBean;
import com.megvii.koala.data.verify.CreatureVerifier;

public class DialogUtil
{
    public IDialogListener listener;

    private Context context;

    private AlertDialog alertDialog;

    public DialogUtil(Context context, IDialogListener listener)
    {
        this.context = context;
        this.listener = listener;
    }

    public void showCreatureDialog(final CreatureBean creature)
    {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setCancelable(true);
        alertDialog.show();
        alertDialog.setCanceledOnTouchOutside(true);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View comment_view = mInflater.inflate(R.layout.dialogvisitor_item, null, false);
        Window window = alertDialog.getWindow();
        window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setContentView(comment_view);

        Button deleteBtn = (Button) comment_view.findViewById(R.id.dialogRemark_item_deleteBtn);
        Button remarkBtn = (Button) comment_view.findViewById(R.id.dialogRemark_item_remarkBtn);

        deleteBtn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                alertDialog.cancel();
                showDeleteDialog(creature);
            }
        });

        remarkBtn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                alertDialog.cancel();
                showRemarkDialog(creature);
            }
        });
    }

    public void showDeleteDialog(final CreatureBean creature)
    {
        alertDialog = new AlertDialog.Builder(context).setTitle("是否确定删除" + creature.name + "?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        if (listener != null)
                        {
                            listener.dialogConfirmDelete(creature);
                        }
                        alertDialog.cancel();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        alertDialog.cancel();
                    }
                }).setCancelable(false).create();
        alertDialog.show();
        alertDialog.setCanceledOnTouchOutside(false);
    }

    public void showRemarkDialog(final CreatureBean creature)
    {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setCancelable(true);
        alertDialog.show();
        alertDialog.setCanceledOnTouchOutside(false);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View comment_view = mInflater.inflate(R.layout.dialogremark_item, null, false);
        Window window = alertDialog.getWindow();
        window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setContentView(comment_view);

        final LimitEditText editText = (LimitEditText) comment_view
                .findViewById(R.id.dialogRemark_item_edit);
        editText.setLimitation(CreatureVerifier.REMARK_MAX);
        if (creature.remark != null)
        {
            editText.setText(creature.remark, false);
        }

        Button sureBtn = (Button) comment_view.findViewById(R.id.dialogRemark_item_sureBtn);
        Button cancleBtn = (Button) comment_view.findViewById(R.id.dialogRemark_item_cancleBtn);

        sureBtn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                String remark = editText.getText().toString();
                if (listener != null)
                {
                    listener.dialogConfirmRemark(creature, remark);
                }
                alertDialog.cancel();
            }
        });

        cancleBtn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                alertDialog.cancel();
            }
        });
    }

    public void onDestory()
    {
        context = null;
    }

    public interface IDialogListener
    {
        public void dialogConfirmDelete(CreatureBean creature);

        public void dialogConfirmRemark(CreatureBean creature, String remark);
    }
}
